//
//  NEDViewController.m
//  NEDNetwork
//
//  Created by 林宁宁 on 12/25/2021.
//  Copyright (c) 2021 林宁宁. All rights reserved.
//

#import "NEDViewController.h"
#import <NEDApp/NEDApp+Mediator.h>
#import "NEDMediatorNetwork.h"
#import "NEDRequest.h"
#import "NEDRequest+Config.h"
#import "NEDRequest+Promise.h"
#import <coobjc/coobjc.h>
#import "NEDAppConfig.h"

@interface NEDVideoInfo : NSObject
@property (copy, nonatomic) NSString * sid;
@property (copy, nonatomic) NSString * text;
@property (copy, nonatomic) NSString * video;
@property (copy, nonatomic) NSString * thumbnail;
@property (copy, nonatomic) NSString * header;
@property (copy, nonatomic) NSString * images;
@property (copy, nonatomic) NSString * name;
@end

@implementation NEDVideoInfo
@end



@interface NEDViewController ()

@end

@implementation NEDViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    APP.mediator.delegateHTTP = [NEDMediatorNetwork new];
    
//    [self requestMethod1];
//    [self requestMethod2];
//    [self requestMethod3];
//    [self requestMethod4];
//    [self requestMethod5];
//    [self requestMethod6];
    [self requestMethod7];
//    [self requestMethod8];
//    [self requestMethod9];
//    [self requestMethod10];
//    [self requestMethod11];
}

/// json数据解析
- (void)requestMethod11{
    COPromise<NEDAppConfig *> * promise = NEDRequestRegister(NEDAppConfig).GET().pathurl(@"api/sys/getConfig").baseurl(@"http://localdev.energy-java.98du.com/").promise;
    [promise then:^id _Nullable(NEDAppConfig * _Nullable value) {
        
        return promise;
    }];
    [promise catch:^(NSError * _Nonnull error) {
        NSLog(@"请求失败了");
    }];
}

/// 请求重试次数设置
- (void)requestMethod10{
    COPromise<NEDVideoInfo *> * promise = NEDRequestRegister(NEDVideoInfo).GET().retryTimes(5).retryTimeInterval(3).fullurl(@"https://api.apiopen.top/getSingleJoke/error?sid=28654780").promise;
    [promise then:^id _Nullable(NEDVideoInfo * _Nullable value) {
        
        return promise;
    }];
    [promise catch:^(NSError * _Nonnull error) {
        NSLog(@"重试请求都失败了");
    }];
}

/// 协程 await 链式
- (void)requestMethod9{
    co_launch(^{
        NSString * string = [NEDRequest new].GET().fullurl(@"https://api.apiopen.top/recommendPoetry/err").await;
        NSLog(@"string = %@",string);
        
        NEDVideoInfo * info1 = NEDRequestRegister(NEDVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780").await;
        NSLog(@"info1 = %@", info1);
        
        NSArray<NEDVideoInfo *> * list = NEDRequestRegisterList(NEDVideoInfo).GET().fullurl(@"https://api.apiopen.top/getJoke?page=1&count=2&type=video").await;
        NSLog(@"list = %@", list);
        
        NSLog(@"finish");
    });
}

/// 协程 请求队列 顺序等待
- (void)requestMethod8{
    co_launch(^{
        NSString * string = await([NEDRequest  new].GET().fullurl(@"https://api.apiopen.top/recommendPoetry").promise);
        NSLog(@"string = %@",string);
        
        NEDVideoInfo * info1 = await(NEDRequestRegister(NEDVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780").promise);
        NSLog(@"info1 = %@", info1);
        
        NSArray<NEDVideoInfo *> * list = await(NEDRequestRegisterList(NEDVideoInfo).GET().fullurl(@"https://api.apiopen.top/getJoke?page=1&count=2&type=video").promise);
        NSLog(@"list = %@", list);
        
        NSLog(@"finish");
    });
}

/// promise 设置请求先后依赖
- (void)requestMethod7{
    COPromise<NEDVideoInfo *> * promise1 = NEDRequestRegister(NEDVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780").promise;
    COPromise<NSArray<NEDVideoInfo *> *> * promise2 = NEDRequestRegisterList(NEDVideoInfo).GET().fullurl(@"https://api.apiopen.top/getJoke?page=1&count=2&type=video").promise;
    COPromise * promise3 = [NEDRequest  new].GET().fullurl(@"https://api.apiopen.top/recommendPoetry/error").promise;
    
    [[[[promise1 then:^id _Nullable(NEDVideoInfo * _Nullable value) {
        return promise2;
    }] then:^id _Nullable(id  _Nullable value) {
        return promise3;
    }] then:^id _Nullable(id  _Nullable value) {
        NSLog(@"%@",value);
        return nil;
    }] catch:^(NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}

/// promise设置请求 回调信息
- (void)requestMethod6{
    COPromise<NEDVideoInfo *> * promise = NEDRequestRegister(NEDVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780").promise;
    [promise then:^id _Nullable(NEDVideoInfo * _Nullable value) {
        
        return promise;
    }];
    [promise catch:^(NSError * _Nonnull error) {
        
    }];
}

/// 泛型设置请求结果 如果为数组类型
- (void)requestMethod5{
    [NEDRequestRegisterList(NEDVideoInfo).fullurl(@"https://api.apiopen.top/getJoke?page=1&count=2&type=video") executeWithSuccess:^(NSString * _Nullable message, NSArray<NEDVideoInfo *> * _Nonnull result) {
        NSLog(@"%@",result);
    }];
}

/// 泛型设置请求结果需要解析的数据类型
/// 请求参数链式配置
- (void)requestMethod4{
    [NEDRequestRegister(NEDVideoInfo).GET().fullurl(@"https://api.apiopen.top/getSingleJoke?sid=28654780") executeWithSuccess:^(NSString * _Nullable message, NEDVideoInfo * _Nonnull result) {
        NSLog(@"%@",result);
    }];
}

/// 通过配置 设置请求 设置请求结果需要解析的数据类型
- (void)requestMethod3{
    [[NEDRequest instanceConfig:^(__kindof NEDRequest * _Nonnull request, NEDRequestConfig * _Nonnull config) {
        config.responseClass = NEDVideoInfo.class;
        config.method = YTKRequestMethodGET;
        config.baseURL = @"https://api.apiopen.top/getSingleJoke?sid=28654780";
    }] executeWithSuccess:^(NSString * _Nullable message, NEDVideoInfo * _Nonnull result) {
        
    }];
}

/// 通过配置 设置请求 baseurl未域名 pathurl 路径设置请求信息
- (void)requestMethod2{
    [[NEDRequest instanceConfig:^(__kindof NEDRequest * _Nonnull request, NEDRequestConfig * _Nonnull config) {
        config.method = YTKRequestMethodGET;
        config.baseURL = @"https://api.apiopen.top";
        config.pathURL = @"getJoke?page=1&count=2&type=video";
    }] executeWithSuccess:^(NSString * _Nullable message, id  _Nonnull result) {
        NSLog(@"%@",result);
    } failure:^(NSString * _Nullable failureMsg, NSInteger errorCode, NSError * _Nonnull error) {
        
    }];
}

/// 通过配置 设置请求 baseurl就是完整地址
- (void)requestMethod1{
    
    [[NEDRequest instanceConfig:^(__kindof NEDRequest * _Nonnull request, NEDRequestConfig * _Nonnull config) {
        config.method = YTKRequestMethodGET;
        config.baseURL = @"https://api.apiopen.top/getJoke?page=1&count=2&type=video";
    }] executeWithSuccess:^(NSString * _Nullable message, id  _Nonnull result) {
        NSLog(@"%@",result);
    } failure:^(NSString * _Nullable failureMsg, NSInteger errorCode, NSError * _Nonnull error) {
        
    }];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
